package org.lisy.java.oop;

/**
 * 反射
 * 
 * @author lisy
 */
public class ReflectOperate {
	
	public static void main(String[] args) throws ClassNotFoundException {
		//以String为例：
		String str="hello world";
		//方式一 ：通过对象的getClass()方法
		Class<?> clazz1 = str.getClass();
		//方式二 ：通过对象的getSuperClass()方法
		Class<?> clazz2 = clazz1.getClass();
		//方式三：通过类的class属性
		Class<?> clazz3 = String.class;
		//方式四：通过Class类的静态方法forName(String className)
		Class<?> clazz4 = Class.forName("java.lang.String");
		//方式五：通过基本类型的包装类.TYPE
		Class<?> clazz5 = Long.TYPE;
		System.out.println(clazz1.getName());
		System.out.println(clazz2.getSuperclass().getName());
		System.out.println(clazz3.getName());
		System.out.println(clazz4.getName());
		System.out.println(clazz5.getName());

		System.out.println("1、通过类本身获得对象");
		Class<?> clazz = str.getClass();
		System.out.println(clazz.getName());
		System.out.println("==============");

		System.out.println("2、通过子类的实例获得父类对象");
		Customer customer = new Customer();
		clazz = customer.getClass();
		System.out.println(customer.getClass().getName());
		System.out.println(clazz.getSuperclass().getName());
		System.out.println("==============");
	}
}

class Customer {
	private Long id;
	private String name;
	private int age;

	public Customer() {
	}

	public Customer(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}
